package com.lagou.boot;

import com.lagou.service.UserServiceImpl;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

public class ServerBoot {
    private static CuratorFramework client;

    private static String address = "127.0.0.1";
    private static int port = 9001;
    public static void main(String[] args) throws Exception {

        //启动服务器
        UserServiceImpl.startServer("127.0.0.1",port);
        initZookeeper();
        client.start();
        createSession();



    }

    public static void initZookeeper(){
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework curator = CuratorFrameworkFactory.builder()
                .connectString("115.159.158.234:2181")//腾讯云地址
                .sessionTimeoutMs(5000)  // 会话超时时间
                .connectionTimeoutMs(5000) // 连接超时时间
                .retryPolicy(retryPolicy)
                .namespace("base") // 包含隔离名称
                .build();
        client = curator;
    }

    public static void createSession() throws Exception {
        String server = "";
        client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL)
                .forPath("/"+address+"/"+port,server.getBytes());
    }















    public static CuratorFramework getClient() {
        return client;
    }

    public static String getAddress() {
        return address;
    }

    public static int getPort() {
        return port;
    }
}
